package com.lvbaoins.creditapp.core.filter;

import org.slf4j.MDC;

import javax.servlet.*;
import java.io.IOException;
import java.util.UUID;

/**
 * @author : gjb
 * @description: 添加日志
 * @date : 2020-11-04 14:41
 * @version: : v1.0
 */
public class Log4jFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //添加MDC日志
        String logtrackId = UUID.randomUUID().toString();
        MDC.put("logTrackId", logtrackId);
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            //移除MDC日志
            MDC.remove(logtrackId);
        }
    }

}
